package com.example.demo2.controller;

import com.example.demo2.entity.*;
import com.example.demo2.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.MalformedURLException;
import javax.servlet.http.HttpSession;
import java.net.URL;
import java.util.List;

@RestController
public class StudyController {

    @Autowired
    private PmcMapper pmcMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private StandardMapper standardMapper;
    @Autowired
    private StudyMapper studyMapper;

    @GetMapping("/findPmcByUID")
    public List<PermissionConnection> findPmcByUID(HttpSession session)
    {
        Integer uID=(Integer) session.getAttribute("uID");
        List<PermissionConnection> permissionConnections = pmcMapper.findPmcByUID(uID);
        for(PermissionConnection permissionConnection:permissionConnections)
        {
            Integer pjId=permissionConnection.getPjId();
            Project project=projectMapper.findProjectByPjId(pjId);
            if(project!=null)
            {
                permissionConnection.setPjName(project.getPjName());
                Standard standard=standardMapper.findStandardByStaId(project.getStaId());
                permissionConnection.setStaName(standard.getStaName());
            }
        }
        return permissionConnections;
    }

    @GetMapping("/downloadPmc")
    public ResponseEntity<Resource> downloadPmc(int pmcId) throws MalformedURLException {
        String pdfUrl = pmcMapper.findPmcUrlByPmcId(pmcId);
        if (pdfUrl == null) {
            return ResponseEntity.notFound().build();
        }
        Resource resource = new UrlResource(new URL(pdfUrl));
        if(!resource.exists()||!resource.isReadable())
        {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION,"attachment;filename=\"" + resource.getFilename() +"\"")
                .contentType(MediaType.APPLICATION_PDF)
                .body(resource);
    }

    @GetMapping("/findStudyByUID")
    public List<Study> findStudyByUID(HttpSession session)
    {
        Integer uID=(Integer) session.getAttribute("uID");
        List<Study> studies = studyMapper.findStudyByUID(uID);
        for(Study study:studies)
        {
            Integer pjId=study.getPjId();
            Project project=projectMapper.findProjectByPjId(pjId);
            if(project!=null)
            {
                study.setPjName(project.getPjName());
                Standard standard=standardMapper.findStandardByStaId(project.getStaId());
                study.setStaName(standard.getStaName());
            }
        }
        return studies;
    }
}
